import com.lmq.onjava.RmDir;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

public class testFileApi {
    static Path src = Paths.get("src/main/java/com/lmq");
    static Path src2 = Paths.get("src");
    static Path src3 = Paths.get("");
    static Random rand = new Random(47);
    static final int SIZE = 1000;

    static void refreshTestDir(Path test1) throws Exception {
        if(Files.exists(test1))
            RmDir.rmdir(test1);
        if(!Files.exists(test1))
            Files.createDirectory(test1);
    }
    public static void newDirectoryStreamTest() throws  Exception {

        DirectoryStream<Path> paths = Files.newDirectoryStream(src);
//        paths.forEach(System.out::println);
        ArrayList<String> vstring = new ArrayList<>();
        vstring.add("hello");
        vstring.add("world");
//        vstring.forEach(x-> System.out.println(x));
        paths.forEach(System.out::println);


        for (String next : vstring) {
            System.out.println(next);
        }
//        for (Path i:paths
//             ) {
//            System.out.println(i);
//
//        }

    }

    public static void readAllLinesTest() throws  Exception {
        List<String> strings = Files.readAllLines(src2.resolve("../Cheese.txt").toRealPath());
        strings.forEach(System.out::println);
    }
    public static void linesTest() throws  Exception {
        Stream<String> strings = Files.lines(src2.resolve("../Cheese.txt").toRealPath());
        strings.forEach(System.out::println);
    }

    public static void writeTest()throws Exception {
        // Write bytes to a file:
        Path destPath = src3.resolve("data").toAbsolutePath();
        refreshTestDir(destPath);
        byte[] bytes = new byte[SIZE];
        rand.nextBytes(bytes);
        Files.write( destPath.resolve("my.dat").toAbsolutePath(), bytes);
    }

    public static void main(String[] args) throws Exception {
//        newDirectoryStreamTest();
//        readAllLinesTest();
//        linesTest();
//        linesTest();
        writeTest();

    }
}
